package com.wongoing.sys.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.wongoing.annotation.Description;
import com.wongoing.base.PageResult;
import com.wongoing.sys.action.BaseAction;
import com.wongoing.sys.model.SysBasSite;
import com.wongoing.sys.service.SysBasSiteService;
import com.wongoing.sys.shiro.ShiroUser;
import com.wongoing.sys.viewmodel.JsonResult;
import com.wongoing.util.Constants;
import com.wongoing.util.FileUtil;
import com.wongoing.util.ImageUtil;
import com.wongoing.util.LogService;
import com.wongoing.util.ModelAndViewConstants;
import com.wongoing.util.PageUtil;
import com.wongoing.util.ResourceUtil;
import com.wongoing.util.Status;

@Controller
@RequestMapping("/sys/sysSiteAction")
public class SysBasSiteAction extends BaseAction {
	private SysBasSiteService service;

	@Autowired
	public void setService(SysBasSiteService service) {
		this.service = service;
	}

	/**
	 * 功能说明：进入列表显示界面
	 * 修改说明：
	 * @author zhaoxihao
	 * @date 2017年10月15日 下午12:26:19
	 * @param pageSize 每页记录数
	 * @param pageCurrent 当前页索引
	 * @param orderField 排序字段
	 * @param orderDirection 排序方向
	 * @param paramCompanyName 参数字段：站点名称
	 * @param paramRemark 参数字段：备注
	 * @return
	 */
	@RequestMapping(value="/toList")
	public ModelAndView toList(Integer pageSize, Integer pageCurrent, String orderField, String orderDirection,String paramSiteName,
			 String paramRemark) {
		ModelAndView mv = null;
		try {
			
			PageResult<SysBasSite> pageResult = PageUtil.pageSet(this.getClass(), pageSize, pageCurrent, orderField, orderDirection);
			pageResult.getParam().put("status", "0");
			//模糊查询判断
			List<Boolean> multiFuzzyQuery = new ArrayList<Boolean>();
		
			if (paramRemark != null && !"".equals(paramRemark)) {
				pageResult.getParam().put("remark", paramRemark);
				pageResult.getExt().put("paramRemark", paramRemark);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			if (paramSiteName != null && !"".equals(paramSiteName)) {
				pageResult.getParam().put("siteName", paramSiteName);
				pageResult.getExt().put("siteName", paramSiteName);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			pageResult.setOrderField("pbs.RecordTime");
			pageResult.setOrderDirection("DESC");
			pageResult = this.service.getSiteListPageResult(pageResult);
			mv = new ModelAndView();
			mv.addObject(ModelAndViewConstants.PAGE_RESULT, pageResult);
			mv.setViewName(ModelAndViewConstants.SITE_MAIN_VIEW);
			LogService.getInstance(this).debug("获取站点分页数据成功："+ModelAndViewConstants.SITE_MAIN_VIEW);
		}
		catch(Exception ex) {
			LogService.getInstance(this).error("获取站点分页数据失败：" + ex.getMessage(), ex);
			mv = new ModelAndView(ModelAndViewConstants.ERROR_VIEW);
		}
		return mv;
	}

	/**
	 * 功能说明：进入添加界面操作
	 * 修改说明：
	 * @author zhaoxihao
	 * @date 2017年10月15日 下午12:28:59
	 * @return
	 */
	@RequestMapping(value="/toAdd")
	public ModelAndView toAdd(){
		ModelAndView mv = null;
		try {
			Map<String,Object> param = new HashMap<String, Object>();
			param.put("status", "0");
			
			mv = new ModelAndView();
			mv.addObject(ModelAndViewConstants.OPERATION_KEY,ModelAndViewConstants.OPERATION_VALUE_ADD);
			mv.addObject(ModelAndViewConstants.TITLE_KEY, "添加站点信息");
			mv.setViewName(ModelAndViewConstants.SITE_EDIT_VIEW);
			LogService.getInstance(this).error("进入站点添加功能成功：" +ModelAndViewConstants.SITE_EDIT_VIEW);
		}
		catch(Exception ex) {
			LogService.getInstance(this).error("进入站点添加失败：" + ex.getMessage(), ex);
			mv = new ModelAndView("redirect:/error.jsp");
		}
		return mv;
	}
	/**
	 * 功能说明：进入编辑界面
	 * 修改说明：
	 * @author zhaoxihao
	 * @date 2017年10月15日 下午12:29:20
	 * @param id 用户id，主键值
	 * @return
	 */
	@RequestMapping(value="/toEdit")
	public ModelAndView toEdit(Integer id) {
		ModelAndView mv = null;
		try {
			SysBasSite site = this.service.getByPrimaryKey(id);
			Map<String,Object> param = new HashMap<String, Object>();
			param.put("status", "0");
		
			mv = new ModelAndView();
			mv.addObject(ModelAndViewConstants.OPERATION_KEY,ModelAndViewConstants.OPERATION_VALUE_EDIT);
			mv.addObject(ModelAndViewConstants.TITLE_KEY, "修改站点信息");
			mv.addObject(ModelAndViewConstants.SITE_MODEL, site);
			mv.setViewName(ModelAndViewConstants.SITE_EDIT_VIEW);
		}
		catch(Exception ex) {
			LogService.getInstance(this).error("进入站点编辑失败：" + ex.getMessage(), ex);
			mv = new ModelAndView(ModelAndViewConstants.ERROR_VIEW);
		}
		return mv;
	}

	
/**
	 * 功能说明：执行删除操作
	 * 修改说明：
	 * @author zhaoxihao
	 * @date 2017年10月15日 下午12:29:51
	 * @param id 用户id，主键值
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/doDel")
	public Map<String, Object> doDel(Integer id) {
		Map<String, Object> result = null;
		try {
			SysBasSite entity = new SysBasSite();
			entity.setId(id);
			entity.setStatus("1");
			int count = this.service.updateByPrimaryKey(entity);
			if (count > 0) {
				LogService.getInstance(this).debug("删除站点信息成功!");
			}
			Integer statusCode = 200;
			String msg = "删除成功!";
			String tabid = tabid(ModelAndViewConstants.SITE_SYS_ID);
			result = JsonResult.jsonReturn(Status.OK.getCode(), msg, tabid,false,"","");
		}
		catch(Exception ex) {
			LogService.getInstance(this).error("删除站点信息[" + id + "]失败：" + ex.getMessage(), ex);
			String msg =  "删除站点信息[" + id + "]失败：" + ex.getMessage();
			result = JsonResult.jsonReturnErr(msg);
		}
		return result;
	}

	
	/**
	 * 
	 * 功能说明： 修改说明：站点信息保存
	 * 
	 * @author zhaoxihao
	 * @date 2017-10-18 上午9:53:47
	 * @param dsType
	 *            数据源类型
	 * @param siteId
	 *            站点Id
	 * @param siteName
	 *            站点名称
	 * @param file
	 *            上传文件
	 * @return
	 */
	@Description("站点信息保存")
	@ResponseBody
	@RequestMapping(value = "/doSave", method = RequestMethod.POST, consumes = "multipart/form-data")
	public Map<String, Object> doSave(@RequestParam(value = "dsType", defaultValue = "sysDs") String dsType,
			@RequestParam(value = "file", required = false) MultipartFile file,
			SysBasSite entity,  String op) {
		Map<String, Object> jsonResult = null;
		String msg = "";
		
		try {
			if (file.getSize() != 0) {
				Map<String, Object> result =  ImageUtil.getInstance().uploadImg(entity.getImgUrl(),Constants.UPLOAD_IMG_SITE, file);
				if (result.get("statusCode") =="300") {
					return result;
				}
				entity.setImgUrl((String)result.get("message"));
			}
			// 存储站点信息
			
				String tabid = tabid(ModelAndViewConstants.SITE_SYS_ID);
				Date now = new Date();
				ShiroUser currentUser = (ShiroUser)SecurityUtils.getSubject().getPrincipal();
				entity.setModifyUserId(currentUser.getUserId());
				entity.setModifyUserName(currentUser.getAccount());
				Subject subject = SecurityUtils.getSubject();
				String sitename = subject.getPrincipal().toString();
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("sitename", sitename);
				if (null == op || ModelAndViewConstants.OPERATION_VALUE_ADD.equals(op)) {
					entity.setStatus("0");
					int count = this.service.insert(entity);
					if (count > 0) {
						LogService.getInstance(this).debug("添加站点信息成功!");
					}
				} else {
					int count = this.service.updateByPrimaryKey(entity);
					if (count > 0) {
						LogService.getInstance(this).debug("编辑站点信息成功!");
					}
				}
				Integer statusCode = 200;
				String siteMsg = "站点信息保存成功";
				jsonResult = JsonResult.jsonReturn(statusCode, siteMsg, tabid);
				return jsonResult;
			
		
		} catch (Exception e) {
			msg = "接口调用异常";
			jsonResult = JsonResult.jsonWsReturn(1, msg, e.getMessage());
			LogService.getInstance(this).error("保存站点信息失败" + e.getMessage());
			String mg =  "保存站点信息失败：" + e.getMessage();
			jsonResult = JsonResult.jsonReturnErr(mg);
			return jsonResult;
		}

	}
}
